//
//  AdvertisingPageViewBuy.swift
//  iModern
//
//  Created by Ralf on 7/26/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class AdvertisingPageViewBuy: TableViewCell
{
    var activity = NSDictionary()

    @IBOutlet var messageButton:   UIButton!
    @IBOutlet var buyButton:       UIButton!
    @IBOutlet var loadStroeButton: UIButton!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        messageButton.layer.borderWidth = 1.0
        messageButton.layer.borderColor = UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1).CGColor

        loadStroeButton.layer.borderWidth = 1.0
        loadStroeButton.layer.borderColor = UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1).CGColor
        buyButton.addTarget(self, action: Selector("buy"), forControlEvents: UIControlEvents.TouchUpInside)
    }

    func setData(data: NSDictionary)
    {
        activity = data
        let article = activity["article"] as! NSDictionary
        
        let price   = article["price"] as! Int
        buyButton.setTitle("立即下单 ¥\(price).00", forState: .Normal)
    }

    func buy()
    {
        let article = activity["article"] as! NSDictionary
        let status = article["status"] as? String
        if status == "Ready for Sale"
        {
            if appDelegate.isLogged()
            {
                let controller = MakeAppointmentFormViewController()
                controller.advertising = activity
                appDelegate.navigationController?.pushViewController(controller, animated: true)
            }
            else
            {
                self.login()
            }
        }
        else
        {
            self.alertWarningMessage("该商品已下架")
            return
        }

    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func sendMessage(sender: UIButton)
    {
        if appDelegate.isLogged()
        {
            let controller = ChatViewController()
            controller.contact = activity.objectForKey("article")?.objectForKey("contact") as! [String:AnyObject]
            pushViewController(controller)
        }
        else
        {
            login()
        }
    }

    @IBAction func loadOtherProfileVIew(sender: UIButton)
    {
        let controller = OtherProfileViewController()
        let userName   = activity.objectForKey("article")?.objectForKey("contact")?.objectForKey("userName") as? String
        controller.userName = userName
        controller.contact = activity.objectForKey("article")?.objectForKey("contact") as? [String:AnyObject]
        pushViewController(controller)
    }
}
